import React from 'react';
import {inject, observer} from 'mobx-react';
import {Button, Spin} from 'antd';
import {
    WeaPopoverHrm,
    WeaLocaleProvider,
    WeaRightMenu,
    WeaTab,
    WeaTop,
    WeaFormItem,
    WeaBrowser,
    WeaSearchGroup,
    WeaSelect,
} from 'ecCom';
import {WeaTableNew} from "comsMobx";
import {renderAlertPage, setWindowKeys} from '../../util/Util';

const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-方案设置查询
 * jiansong 2019/6/17
 */
@inject("programListStore")
@observer
export default class ProgramList extends React.Component {

    constructor(props) {
        super(props);
        //tab操作后更新列表数据
        window.RefreshProgramList = () => {
            this.props.programListStore.tableStore.reloadCurrent();
            this.props.updateNum();
            this.props.programListStore.getAccessAuditProgramNumData();
        }
        this.state = {
            collectParams: {
                favname: getLabel(506165, "方案设置查询"),
                favouritetype: "5",
                link: '/spa/workrelate/static/index.html#/main/workrelate/performance/performanceIndex?menutype=5',
                importantlevel: 1,
            }
        };
        this.store = this.props.programListStore;
    }

    componentWillMount() {
        setWindowKeys(5, getLabel(506165, "方案设置查询"));
        if (this.props.type) {
            this.store.queryParams.type = this.props.type;
        } else {
            this.store.queryParams.type = null;
        }
        this.store.doInit();
    }

    componentWillUnmount() {
        window.RefreshProgramList = null
    }

    render() {
        const {collectParams} = this.state;
        const {loading, stabList, tableStore, showSearchAd, getProgramList, doSearch, setShowSearchAd} = this.store;
        if (!loading && stabList && stabList.length == 0) {
            return renderAlertPage('<span style="color:red">' + getLabel(508557, "暂未开启任何考核周期") + '</span>','','','',this.props.ecId || '')
        }
        const rightMenu = [{
            key: 'SEARCH',
            icon: <i className='icon-coms-search'/>,
            content: getLabel(388113, '搜索'),
            onClick: doSearch,
        }];

        return (
            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@z60zky`} spinning={loading}>
                <div className="wea-workrelate-performance-programlist" style={{height: "100vh"}}>
                    <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@fx2leg`}/>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@f6jrwp`} datas={rightMenu} collectParams={collectParams}>
                        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@seiguq`}
                            title={getLabel('506165', '方案设置查询')}
                            icon={<i className='icon-coms-implement'/>}
                            iconBgcolor='rgb(150, 53, 138)'
                            buttons={[
                                (this.condition()),
                            ]}
                            showDropIcon={true}
                            dropMenuDatas={rightMenu}
                        >
                            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@c2ixcr`}
                                searchType={['advanced']}
                                showSearchAd={showSearchAd}
                                setShowSearchAd={bool => setShowSearchAd(bool)}
                                searchsAd={this.getSearchsAd()}
                                hideSearchAd={() => setShowSearchAd(false)}
                                buttonsAd={this.getTabButtonsAd(this.props.ecId || '')}
                                onSearch={getProgramList}
                            />
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@x71c3h`}
                                comsWeaTableStore={tableStore}
                                hasOrder={true}
                                needScroll={true}
                            />
                        </WeaTop>
                    </WeaRightMenu>
                </div>
            </Spin>
        )
    }

    //考核方案未设置提醒
    condition() {
        const {programListStore: {amount1, amount2, amount3, amount4}} = this.props;
        if (amount1 > 0 || amount2 > 0 || amount3 > 0 || amount4 > 0) {
            return (
                <div>
                    <a href='javascript:void(0)'
                       onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/programIndex?type=3")}>{getLabel('509098', '考核方案未设置提示')}</a>（
                    {amount1 > 0 && (
                        <span>{getLabel('17138', '年度')}：
                                <a href='javascript:void(0)'
                                   onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/programIndex?queryType=nofyear")}>{amount1}</a>
                            </span>)
                    }
                    {amount2 > 0 && (
                        <span style={{marginLeft: "5px"}}>{getLabel('20729', '半年')}：
                                <a href='javascript:void(0)'
                                   onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/programIndex?queryType=nohyear")}>{amount2}</a>
                            </span>)
                    }
                    {amount3 > 0 && (
                        <span style={{marginLeft: "5px"}}>{getLabel('17495', '季度')}：
                                <a href='javascript:void(0)'
                                   onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/programIndex?queryType=noquarter")}>{amount3}</a>
                            </span>)
                    }
                    {amount4 > 0 && (
                        <span style={{marginLeft: "5px"}}>{getLabel('19398', '月度')}：
                                <a href='javascript:void(0)'
                                   onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/programIndex?queryType=nomonth")}>{amount4}</a>
                            </span>)
                    }
                    {(<span>）</span>)}
                </div>
            )
        }
    }

    //高级搜索条目
    getSearchsAd() {
        let {queryType, queryParams, onSearchChange, hrmData, subcompanyData, departmentData} = this.store
        let items = [];

        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@je4ch1@person`} label={getLabel('30042', '人员')} ratio1to2>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@ags3mo@person`}
                        type={17}
                        isSingle={false}
                        title={getLabel('179', '人力资源')}
                        replaceDatas={hrmData}
                        onChange={(ids, names) => onSearchChange({'hrmids': ids, 'hrmnames': names})}
                    />
                </WeaFormItem>
        });
        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@p1lfsp@subdata`} label={getLabel('33553', '分部')} ratio1to2>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@102pr8@subdata`}
                        type={194}
                        title={getLabel('33553', '分部')}
                        icon="icon-coms-hrm"
                        replaceDatas={subcompanyData}
                        onChange={(ids, names) => onSearchChange({'subcompanyids': ids, 'subcompanynames': names})}
                    />
                </WeaFormItem>
        });
        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@5r0th4@deptdata`} label={getLabel(27511, '部门')} ratio1to2>
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@q0mote@deptdata`}
                        type={57}
                        title={getLabel(27511, '部门')}
                        icon="icon-coms-hrm"
                        replaceDatas={departmentData}
                        onChange={(ids, names) => onSearchChange({'departmentids': ids, 'departmentnames': names})}
                    />
                </WeaFormItem>
        });
        items.push({
            com:
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@utxafp@type`} label={getLabel('500401', '类型')} ratio1to2>
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@bnabw9@type`}
                        options={queryType}
                        fieldName={'title'}
                        value={queryParams.type}
                        detailtype={2}
                        onChange={(val) => onSearchChange({'type': val})}
                    />
                </WeaFormItem>
        });
        return <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ddp540`} title={getLabel('32905', '常用条件')} items={items} showGroup={true} col={2}/>
    }

    //高级搜索框按钮
    getTabButtonsAd(ecId) {
        const {doSearch, resetSearchAd, setShowSearchAd} = this.store;
        return [
            (<Button ecId={`${ecId}_Button@arwlke@search`} type="primary" onClick={doSearch}>{getLabel('388113', '搜索')}</Button>),
            (<Button ecId={`${ecId}_Button@h7nm2v@reset`} type="ghost" onClick={resetSearchAd}>{getLabel('2022', '重置')}</Button>),
            (<Button ecId={`${ecId}_Button@me082k@cancel`} type="ghost" onClick={() => setShowSearchAd(false)}>{getLabel('31129', '取消')}</Button>)
        ]
    }

};